using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WebApiClient.AuthTokens;

namespace ConsoleApp
{
    class TokenFilter : AuthTokenFilter
    {    /// <summary>
         /// 获取提供Token获取的Url节点
         /// </summary>
        public string TokenEndpoint { get; private set; }
        public string Account { get; private set; }
        public string Password { get; private set; }

        /// <summary>
        /// OAuth授权的token过滤器
        /// </summary>
        /// <param name="tokenEndPoint">提供Token获取的Url节点</param>
        /// <param name="client_id">客户端id</param>
        /// <param name="client_secret">客户端密码</param>
        public TokenFilter(string tokenEndPoint, string client_id, string client_secret)
        {
            this.TokenEndpoint = tokenEndPoint ?? throw new ArgumentNullException(nameof(tokenEndPoint));
            this.Account = client_id ?? throw new ArgumentNullException(nameof(client_id));
            this.Password = client_secret ?? throw new ArgumentNullException(nameof(client_secret));
        }

        protected override async Task<TokenResult> RequestTokenResultAsync()
        {
            var tokenClient = new TokenClient(this.TokenEndpoint);
            return await tokenClient.RequestClientCredentialsAsync(this.Account, this.Password);
        }

        protected override async Task<TokenResult> RequestRefreshTokenAsync(string refresh_token)
        {
            var tokenClient = new TokenClient(this.TokenEndpoint);
            return await tokenClient.RequestRefreshTokenAsync(this.Account, this.Password, refresh_token);
        }
    }
}
